import { supabase } from './supabase';
import { getCurrentSession, getCurrentUser } from '@/utils/auth';
import { testResultStorage } from '@/utils/localStorage';
import type { User, VerificationCode, TestResult, Report, Order, DeepSeekAnalysis, OrderItem } from '@/types/types';

// 用户相关API
export const userApi = {
  // 创建或获取用户
  async upsertUser(email: string): Promise<User | null> {
    try {
      console.log('[upsertUser] 开始处理邮箱:', email);
      
      // 方法 1: 尝试直接访问 users 表
      console.log('[upsertUser] 尝试直接访问 users 表...');
      
      // 首先尝试获取现有用户
      const { data: existingUser, error: fetchError } = await supabase
        .from('users')
        .select('*')
        .eq('email', email)
        .maybeSingle();
      
      console.log('[upsertUser] 查询结果:', { existingUser, fetchError });
      
      // 如果用户已存在，直接返回
      if (existingUser) {
        console.log('[upsertUser] 用户已存在:', existingUser);
        return existingUser;
      }
      
      // 如果查询出错但不是"未找到"错误，记录错误
      if (fetchError && fetchError.code !== 'PGRST116') {
        console.error('[upsertUser] 查询用户失败:', fetchError);
        
        // 如果是缓存问题 (PGRST205)，尝试使用 Edge Function
        if (fetchError.code === 'PGRST205') {
          console.log('[upsertUser] 检测到缓存问题，尝试使用 Edge Function...');
          try {
            const { data: edgeData, error: edgeError } = await supabase.functions.invoke('upsert-user', {
              body: { email }
            });
            
            if (!edgeError && edgeData && edgeData.data) {
              console.log('[upsertUser] Edge Function 成功:', edgeData.data);
              return edgeData.data as User;
            }
            console.error('[upsertUser] Edge Function 失败:', edgeError);
          } catch (edgeErr) {
            console.error('[upsertUser] Edge Function 异常:', edgeErr);
          }
        }
        
        return null;
      }
      
      // 用户不存在，创建新用户
      console.log('[upsertUser] 用户不存在，创建新用户...');
      const { data: newUser, error: insertError } = await supabase
        .from('users')
        .insert({ email })
        .select()
        .maybeSingle();
      
      console.log('[upsertUser] 创建结果:', { newUser, insertError });
      
      if (insertError) {
        console.error('[upsertUser] 创建用户失败:', insertError);
        
        // 如果是缓存问题，尝试使用 Edge Function
        if (insertError.code === 'PGRST205') {
          console.log('[upsertUser] 检测到缓存问题，尝试使用 Edge Function...');
          try {
            const { data: edgeData, error: edgeError } = await supabase.functions.invoke('upsert-user', {
              body: { email }
            });
            
            if (!edgeError && edgeData && edgeData.data) {
              console.log('[upsertUser] Edge Function 成功:', edgeData.data);
              return edgeData.data as User;
            }
            console.error('[upsertUser] Edge Function 失败:', edgeError);
          } catch (edgeErr) {
            console.error('[upsertUser] Edge Function 异常:', edgeErr);
          }
        }
        
        return null;
      }
      
      console.log('[upsertUser] 用户创建成功:', newUser);
      return newUser;
    } catch (error) {
      console.error('[upsertUser] 异常:', error);
      return null;
    }
  },

  // 根据邮箱获取用户
  async getUserByEmail(email: string): Promise<User | null> {
    const { data, error } = await supabase
      .from('users')
      .select()
      .eq('email', email)
      .maybeSingle();
    
    if (error) {
      console.error('Error getting user:', error);
      return null;
    }
    return data;
  }
};

// 验证码相关API
export const verificationApi = {
  // 创建验证码
  async createVerificationCode(email: string, code: string): Promise<VerificationCode | null> {
    try {
      const expiresAt = new Date();
      expiresAt.setMinutes(expiresAt.getMinutes() + 5); // 5分钟后过期

      const { data, error } = await supabase
        .from('verification_codes')
        .insert({
          email,
          code,
          expires_at: expiresAt.toISOString(),
          used: false
        })
        .select()
        .maybeSingle();
      
      if (error) {
        console.error('Error creating verification code:', error);
        throw new Error(`数据库错误: ${error.message}`);
      }
      return data;
    } catch (error) {
      console.error('Exception in createVerificationCode:', error);
      throw error;
    }
  },

  // 验证验证码
  async verifyCode(email: string, code: string): Promise<boolean> {
    const { data, error } = await supabase
      .from('verification_codes')
      .select()
      .eq('email', email)
      .eq('code', code)
      .eq('used', false)
      .gt('expires_at', new Date().toISOString())
      .order('created_at', { ascending: false })
      .limit(1)
      .maybeSingle();
    
    if (error || !data) {
      console.error('Error verifying code:', error);
      return false;
    }

    // 标记验证码为已使用
    await supabase
      .from('verification_codes')
      .update({ used: true })
      .eq('id', data.id);

    return true;
  }
};

// 测试结果相关API
export const testResultApi = {
  // 创建测试结果
  async createTestResult(userId: string): Promise<TestResult | null> {
    const id = typeof crypto !== 'undefined' && 'randomUUID' in crypto
      ? crypto.randomUUID()
      : 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
          const r = Math.random() * 16 | 0;
          const v = c === 'x' ? r : (r & 0x3 | 0x8);
          return v.toString(16);
        });
    const record: any = {
      id,
      user_id: userId,
      personality_scores: null,
      math_finance_scores: null,
      risk_preference_scores: null,
      investment_style: null,
      euclidean_distance: null,
      created_at: new Date().toISOString(),
      completed_at: null
    };
    try { testResultStorage.saveTestResult(record); } catch (_) {}
    return record;
  },

  // 更新测试结果
  async updateTestResult(id: string, updates: Partial<TestResult>): Promise<TestResult | null> {
    const updated = testResultStorage.updateTestResult(id, {
      ...updates,
      completed_at: updates.completed_at ?? new Date().toISOString(),
    } as any);
    return updated;
  },

  // 获取用户的测试结果
  async getUserTestResults(userId: string): Promise<TestResult[]> {
    const list = testResultStorage.getUserTestResults(userId);
    return list.sort((a: any, b: any) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime());
  },

  // 获取单个测试结果
  async getTestResult(id: string): Promise<TestResult | null> {
    return testResultStorage.getTestResultById(id);
  },

  // 删除测试结果
  async deleteTestResult(id: string): Promise<boolean> {
    try { testResultStorage.deleteTestResult(id); return true; } catch (_) { return false; }
  },

  // 删除用户所有测试结果
  async deleteAllUserTestResults(userId: string): Promise<boolean> {
    try { testResultStorage.deleteAllUserTestResults(userId); return true; } catch (_) { return false; }
  }
};

// 报告相关API
export const reportApi = {
  // 创建报告
  async createReport(userId: string, testResultId: string, reportData: any): Promise<Report | null> {
    const expiresAt = new Date();
    expiresAt.setHours(expiresAt.getHours() + 24); // 24小时后过期

    const { data, error } = await supabase
      .from('reports')
      .insert({
        user_id: userId,
        test_result_id: testResultId,
        report_data: reportData,
        expires_at: expiresAt.toISOString()
      })
      .select()
      .maybeSingle();
    
    if (error) {
      console.error('Error creating report:', error);
      return null;
    }
    return data;
  },

  // 获取报告
  async getReport(id: string): Promise<Report | null> {
    const { data, error } = await supabase
      .from('reports')
      .select()
      .eq('id', id)
      .gt('expires_at', new Date().toISOString())
      .maybeSingle();
    
    if (error) {
      console.error('Error getting report:', error);
      return null;
    }
    return data;
  },

  // 获取用户的所有有效报告
  async getUserReports(userId: string): Promise<Report[]> {
    const { data, error } = await supabase
      .from('reports')
      .select()
      .eq('user_id', userId)
      .gt('expires_at', new Date().toISOString())
      .order('created_at', { ascending: false });
    
    if (error) {
      console.error('Error getting user reports:', error);
      return [];
    }
    return Array.isArray(data) ? data : [];
  }
};

// 支付相关API
export const paymentApi = {
  // 创建Stripe支付会话
  async createCheckoutSession(items: OrderItem[], testResultId: string): Promise<{ url: string; sessionId: string; orderId: string } | null> {
    try {
      const { data: { session } } = await getCurrentSession();
      
      const { data, error } = await supabase.functions.invoke('create_stripe_checkout', {
        body: {
          items,
          test_result_id: testResultId,
          currency: 'cny',
          payment_method_types: ['card']
        },
        headers: session?.access_token ? {
          Authorization: `Bearer ${session.access_token}`
        } : {}
      });

      if (error) {
        console.error('Error creating checkout session:', error);
        return null;
      }

      return data.data;
    } catch (error) {
      console.error('Error creating checkout session:', error);
      return null;
    }
  },

  // 验证支付状态
  async verifyPayment(sessionId: string): Promise<any> {
    try {
      const { data, error } = await supabase.functions.invoke('verify_stripe_payment', {
        body: { sessionId }
      });

      if (error) {
        console.error('Error verifying payment:', error);
        return null;
      }

      return data.data;
    } catch (error) {
      console.error('Error verifying payment:', error);
      return null;
    }
  },

  // 获取用户订单列表
  async getUserOrders(userId: string): Promise<Order[]> {
    const { data, error } = await supabase
      .from('orders')
      .select()
      .eq('user_id', userId)
      .order('created_at', { ascending: false });
    
    if (error) {
      console.error('Error getting user orders:', error);
      return [];
    }
    return Array.isArray(data) ? data : [];
  },

  // 根据测试结果ID获取已完成的订单
  async getCompletedOrderByTestResult(testResultId: string): Promise<Order | null> {
    const isUuidV4 = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(testResultId);
    if (!isUuidV4) {
      return null;
    }
    const { data, error } = await supabase
      .from('orders')
      .select()
      .eq('test_result_id', testResultId)
      .eq('status', 'completed')
      .order('created_at', { ascending: false })
      .limit(1)
      .maybeSingle();
    
    if (error) {
      console.error('Error getting completed order:', error);
      return null;
    }
    return data;
  }
};

// DeepSeek分析相关API
export const deepseekApi = {
  // 生成DeepSeek分析（testData从本地存储传入）
  async generateAnalysis(testResultId: string, orderId: string, testData: any, language: 'zh' | 'en' = 'zh'): Promise<DeepSeekAnalysis | null> {
    try {
      const { data: { session } } = await getCurrentSession();
      
      if (!session?.access_token) {
        console.error('User not authenticated');
        return null;
      }

      const { data, error } = await supabase.functions.invoke('generate_deepseek_analysis', {
        body: {
          testResultId,
          orderId,
          testData,
          language
        },
        headers: {
          Authorization: `Bearer ${session.access_token}`
        }
      });

      if (error) {
        console.error('Error generating analysis:', error);
        return null;
      }

      return data.data.analysis;
    } catch (error) {
      console.error('Error generating analysis:', error);
      return null;
    }
  },

  async generateAnalysisFree(testResultId: string, testData: any, language: 'zh' | 'en' = 'zh'): Promise<DeepSeekAnalysis | null> {
    try {
      const { data: { session } } = await getCurrentSession();
      if (!session?.access_token) {
        console.error('User not authenticated');
        return null;
      }
      const { data, error } = await supabase.functions.invoke('generate_deepseek_analysis_free', {
        body: { testResultId, testData, language },
        headers: { Authorization: `Bearer ${session.access_token}` }
      });
      if (error) {
        console.error('Error generating free analysis:', error);
        return null;
      }
      return data.data.analysis;
    } catch (error) {
      console.error('Error generating free analysis:', error);
      return null;
    }
  },

  // 获取测试结果的DeepSeek分析
  async getAnalysisByTestResult(testResultId: string): Promise<DeepSeekAnalysis | null> {
    const { data, error } = await supabase
      .from('deepseek_analyses')
      .select()
      .eq('test_result_id', testResultId)
      .order('created_at', { ascending: false })
      .limit(1)
      .maybeSingle();
    
    if (error) {
      console.error('Error getting analysis:', error);
      return null;
    }
    return data;
  },

  // 获取用户的所有DeepSeek分析
  async getUserAnalyses(userId: string): Promise<DeepSeekAnalysis[]> {
    const { data, error } = await supabase
      .from('deepseek_analyses')
      .select()
      .eq('user_id', userId)
      .order('created_at', { ascending: false });
    
    if (error) {
      console.error('Error getting user analyses:', error);
      return [];
    }
    return Array.isArray(data) ? data : [];
  }
};
